package handlers

import (
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"

	"chat-app/internal/app/services"
)

// ChatHandler 聊天处理器
type ChatHandler struct {
	chatService services.ChatService
}

func NewChatHandler(chatService services.ChatService) *ChatHandler {
	return &ChatHandler{chatService: chatService}
}

// SendPrivateMessage 发送私聊消息
func (h *ChatHandler) SendPrivateMessage(c *gin.Context) {
	currentUserID := c.MustGet("userID").(uint)

	recipientID, err := strconv.ParseUint(c.Param("recipientID"), 10, 32)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid recipient ID"})
		return
	}

	var req struct {
		Content string `json:"content" binding:"required"`
	}
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
		return
	}

	message, err := h.chatService.SendPrivateMessage(currentUserID, uint(recipientID), req.Content)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusCreated, message)
}

// GetPrivateHistory 获取私聊历史
func (h *ChatHandler) GetPrivateHistory(c *gin.Context) {
	currentUserID := c.MustGet("userID").(uint)

	recipientID, err := strconv.ParseUint(c.Param("recipientID"), 10, 32)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid recipient ID"})
		return
	}

	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "50"))

	messages, err := h.chatService.GetPrivateHistory(currentUserID, uint(recipientID), page, pageSize)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, messages)
}

// CreateGroup 创建群组
func (h *ChatHandler) CreateGroup(c *gin.Context) {
	currentUserID := c.MustGet("userID").(uint)

	var req struct {
		Name string `json:"name" binding:"required"`
	}
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
		return
	}

	group, err := h.chatService.CreateGroup(req.Name, currentUserID)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusCreated, group)
}

// AddGroupMember 添加群成员
func (h *ChatHandler) AddGroupMember(c *gin.Context) {
	currentUserID := c.MustGet("userID").(uint)

	groupID, err := strconv.ParseUint(c.Param("groupID"), 10, 32)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid group ID"})
		return
	}

	userID, err := strconv.ParseUint(c.Param("userID"), 10, 32)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid user ID"})
		return
	}

	if err := h.chatService.AddGroupMember(uint(groupID), uint(userID), currentUserID); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"message": "Member added successfully"})
}

// RemoveGroupMember 移除群成员
func (h *ChatHandler) RemoveGroupMember(c *gin.Context) {
	currentUserID := c.MustGet("userID").(uint)

	groupID, err := strconv.ParseUint(c.Param("groupID"), 10, 32)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid group ID"})
		return
	}

	userID, err := strconv.ParseUint(c.Param("userID"), 10, 32)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid user ID"})
		return
	}

	if err := h.chatService.RemoveGroupMember(uint(groupID), uint(userID), currentUserID); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"message": "Member removed successfully"})
}

// SendGroupMessage 发送群聊消息
func (h *ChatHandler) SendGroupMessage(c *gin.Context) {
	currentUserID := c.MustGet("userID").(uint)

	groupID, err := strconv.ParseUint(c.Param("groupID"), 10, 32)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid group ID"})
		return
	}

	var req struct {
		Content string `json:"content" binding:"required"`
	}
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
		return
	}

	message, err := h.chatService.SendGroupMessage(currentUserID, uint(groupID), req.Content)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusCreated, message)
}

// GetGroupHistory 获取群聊历史
func (h *ChatHandler) GetGroupHistory(c *gin.Context) {
	groupID, err := strconv.ParseUint(c.Param("groupID"), 10, 32)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid group ID"})
		return
	}

	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "50"))

	messages, err := h.chatService.GetGroupHistory(uint(groupID), page, pageSize)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, messages)
}

// DeleteMessage 删除消息
func (h *ChatHandler) DeleteMessage(c *gin.Context) {
	currentUserID := c.MustGet("userID").(uint)

	messageID, err := strconv.ParseUint(c.Param("messageID"), 10, 32)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid message ID"})
		return
	}

	if err := h.chatService.DeleteMessage(uint(messageID), currentUserID); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"message": "Message deleted"})
}

// RecallMessage 撤回消息
func (h *ChatHandler) RecallMessage(c *gin.Context) {
	currentUserID := c.MustGet("userID").(uint)

	messageID, err := strconv.ParseUint(c.Param("messageID"), 10, 32)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid message ID"})
		return
	}

	if err := h.chatService.RecallMessage(uint(messageID), currentUserID); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"message": "Message recalled"})
}
